import { defineStore } from 'pinia'
import type { UserAddress } from '@/types/user-address'
import { ref } from 'vue'


export const useUserAddress = defineStore('address', () => {

    // 储存用户地址
    const addressData = ref<UserAddress>({
        user: '',
        telephone: '',
        dormitory: ''
    })

    // 更新用户数据
    const updateAddress = (newData: typeof addressData.value) => {
        addressData.value = newData
    }

    //   清空数据
    const dearAddress = () => {
        addressData.value = { user: '', telephone: '', dormitory: '' }
    }

    return {
        addressData,
        updateAddress,
        dearAddress
    }
}, {
    // persist: true // 启用持久化存储  网页端
    // 配置持久化 调整为兼容多端的API
    persist: {
        storage: {
            getItem(key) {
                return uni.getStorageSync(key)
            },
            setItem(key, value) {
                uni.setStorageSync(key, value)
            },
        },
    }
})